// main layer
var g_emiTower=null;
var emiTower = cc.Layer.extend({
    _towers : [],
    _jsonObject : null,
    _localFlag : null,
    ctor:function ( jsonfilename ) {
        this._super();
        g_emiTower=this;
        this._jsonObject=CreateJsonWithFile(jsonfilename);
        // add local flag
        this.addLocalFlag();
        // add map
        this.addTowerMenu();
        return true;
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        this._towers.splice(0,this._towers.length);
        this._super();
    },
    addLocalFlag : function(){
        this._localFlag = new cc.Sprite( "res/ok.jpg" );
        this._localFlag.setPosition( cc.p( cc.winSize.width*0.5,cc.winSize.height*0.5 ) );
        this._localFlag.setVisible(false);
        this.addChild(this._localFlag);
    },
    addTowerMenu : function(position){
        var jsonObject=this._jsonObject;
        if(jsonObject) {
            var towers=[];
            var gold = getUILayerInstance().getMoney();
            for ( var i=0;i<jsonObject.tower.length;i++){
                var data=jsonObject.tower[i];
                if( data.tRes ){
                    var resName = "res/"+data.tRes;
                    var tower = new cc.MenuItemImage(resName,resName,this.onChooseTower,this);
                    tower.setPosition( cc.p(-100,-100) );
                    if ( gold<data.cst){
                        this.setTowerEnable(tower,false);
                    }else{
                        this.setTowerEnable(tower,true);
                    }
                    this._towers.push(tower);
                    towers.push(tower);
                    tower.setUserData( data );
                }
            }
            var menu = cc.Menu.create( towers );
            this.addChild( menu );
            // setting position
            menu.setPosition( cc.p(0,0) );
        }else{
            cc.log("json object is invalid");
        }
    },
    onChooseTower : function(sender){
        var iu = getUILayerInstance();
        var data = sender.getUserData();
        var cost = data.cst;
        if ( iu.getMoney()>=cost){
            iu.addMoney( -cost );
            this.addTower( data );
            this.refreshTower();
        }else{
            cc.log("no money");
        }
    },
    // layout towers
    layoutTower : function(basePos){
        for ( var i=0;i<this._towers.length;i++){
            var tower=this._towers[i];
            tower.setPosition( cc.p(basePos.x-(this._towers.length-2)*0.5*80+i*80,basePos.y+110) );
        }
    },
    hideTower : function(){
        for ( var i=0;i<this._towers.length;i++){
            var tower=this._towers[i];
            tower.setPosition( cc.p(-100,-100) );
        }
    },
    // refresh enable towers
    refreshTower : function(){
        var iu = getUILayerInstance();
        var gold=iu.getMoney();
        for ( var i=0;i<this._towers.length;i++){
            var tower=this._towers[i];
            if(tower){
                var cost=tower.getUserData().cst;
                if ( gold<cost){
                    this.setTowerEnable(tower,false);
                }else{
                    this.setTowerEnable(tower,true);
                }
            }
        }
    },
    setTowerEnable : function(tower,enable){
        if(enable){
            tower.setColor(cc.color.WHITE);
        }else{
            tower.setColor(cc.color(135,123,103));
        }
    },

    emission:function(touch){
        if( !this._localFlag.isVisible() ){
            this.refreshTower();
            var local = touch.getLocation();
            var mainLayer=getMainLayerInstance();
            var jsonObj = mainLayer._jsonObject;
            var point = PixelToPoint(jsonObj,local);
            this._localFlag.setPosition(cc.p((point.x+0.5)*jsonObj.width,
                (point.y+0.5)*jsonObj.height));
            this._localFlag.setVisible(true);
            // checking is good to locating tower
            var ret = mainLayer._jsonMap.isEmptyPoint( point );
            if( ret ){
                var pixel = pointToPixel(jsonObj,point);
                getEmiTowerInstance().layoutTower(pixel);
            }else{
                cc.log("this position is not empty");
            }
        }else{
            this._localFlag.setVisible(false);
            this.hideTower( );
        }
    },
    addTower : function( data ){
        cc.log("add tower");
        var mainLayer=getMainLayerInstance();
        var localObj = this._localFlag;
        var resName = "res/"+data.tRes;
        var tower = TowerA.create( resName ,data );
        tower.setPosition( localObj.getPosition() );
        mainLayer.addChild( tower,localObj.getZOrder() );
        this._localFlag.setVisible(false);
        this.hideTower();
    }
});


